/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.google.common.base.Charsets;
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.ImmutableSet;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.mime.ContentDisposition;
/*     */ import com.zimbra.common.mime.ContentType;
/*     */ import com.zimbra.common.mime.MimeCompoundHeader;
/*     */ import com.zimbra.common.mime.MimeConstants;
/*     */ import com.zimbra.common.mime.MimeHeader;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.CharsetUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.index.Fragment;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Document;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailSender.SafeSendFailedException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.CalendarMailSender;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mime.MailboxBlobDataSource;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.service.UploadDataSource;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.service.formatter.VCard;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.DocumentHandler;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.SendFailedException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ import javax.mail.internet.MimePart;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ParseMimeMessage
/*     */ {
/*     */   private static final long DEFAULT_MAX_SIZE = 10485760L;
/*     */   static InviteParser NO_INV_ALLOWED_PARSER;
/*     */   private static final Map<String, String> FETCH_CONTACT_PARAMS;
/*     */   
/*     */   private static class ForceBase64MimeBodyPart
/*     */     extends ZMimeBodyPart
/*     */   {
/*     */     protected void updateHeaders()
/*     */       throws MessagingException
/*     */     {
/* 108 */       super.updateHeaders();
/* 109 */       if (LC.text_attachments_base64.booleanValue()) {
/* 110 */         String ct = Mime.getContentType(this);
/* 111 */         if ((!ct.startsWith("message/")) && (!ct.startsWith("multipart/"))) {
/* 112 */           setHeader("Content-Transfer-Encoding", "base64");
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static MimeMessage importMsgSoap(Element msgElem) throws ServiceException
/*     */   {
/* 120 */     assert (msgElem.getName().equals("m"));
/*     */     
/* 122 */     Element contentElement = msgElem.getElement("content");
/*     */     
/*     */     byte[] content;
/*     */     try
/*     */     {
/* 127 */       String text = StringUtil.lfToCrlf(contentElement.getText());
/* 128 */       content = text.getBytes("utf-8");
/*     */     } catch (UnsupportedEncodingException e) {
/* 130 */       throw ServiceException.FAILURE("encoding error", e);
/*     */     }
/* 132 */     long maxSize = Provisioning.getInstance().getConfig().getLongAttr("zimbraMtaMaxMessageSize", 10485760L);
/*     */     
/* 134 */     if ((maxSize != 0L) && (content.length > maxSize)) {
/* 135 */       throw ServiceException.INVALID_REQUEST("inline message too large", null);
/*     */     }
/*     */     
/* 138 */     InputStream messageStream = new SharedByteArrayInputStream(content);
/*     */     try {
/* 140 */       return new Mime.FixedMimeMessage(JMSession.getSession(), messageStream);
/*     */     } catch (MessagingException me) {
/* 142 */       throw ServiceException.FAILURE("MessagingExecption", me);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   static abstract class InviteParser
/*     */   {
/*     */     private ParseMimeMessage.InviteParserResult mResult;
/*     */     
/*     */     protected abstract ParseMimeMessage.InviteParserResult parseInviteElement(ZimbraSoapContext paramZimbraSoapContext, OperationContext paramOperationContext, Account paramAccount, Element paramElement)
/*     */       throws ServiceException;
/*     */     
/*     */     public final ParseMimeMessage.InviteParserResult parse(ZimbraSoapContext zsc, OperationContext octxt, Account account, Element invElement)
/*     */       throws ServiceException
/*     */     {
/* 157 */       this.mResult = parseInviteElement(zsc, octxt, account, invElement);
/* 158 */       return this.mResult;
/*     */     }
/*     */     
/*     */     public ParseMimeMessage.InviteParserResult getResult() {
/* 162 */       return this.mResult;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   static class InviteParserResult
/*     */   {
/*     */     public ZCalendar.ZVCalendar mCal;
/*     */     
/*     */     public String mUid;
/*     */     
/*     */     public String mSummary;
/*     */     
/*     */     public Invite mInvite;
/*     */   }
/*     */   
/*     */ 
/*     */   public static class MimeMessageData
/*     */   {
/*     */     public List<FileUploadServlet.Upload> fetches;
/*     */     
/*     */     public List<FileUploadServlet.Upload> uploads;
/*     */     
/*     */     public String iCalUUID;
/*     */     
/*     */     void addUpload(FileUploadServlet.Upload up)
/*     */     {
/* 189 */       if (this.uploads == null) {
/* 190 */         this.uploads = new ArrayList(4);
/*     */       }
/* 192 */       this.uploads.add(up);
/*     */     }
/*     */     
/*     */     void addFetch(FileUploadServlet.Upload up) {
/* 196 */       if (this.fetches == null) {
/* 197 */         this.fetches = new ArrayList(4);
/*     */       }
/* 199 */       this.fetches.add(up);
/*     */     }
/*     */   }
/*     */   
/*     */   public static MimeMessage parseMimeMsgSoap(ZimbraSoapContext zsc, OperationContext octxt, Mailbox mbox, Element msgElem, MimeBodyPart[] additionalParts, MimeMessageData out)
/*     */     throws ServiceException
/*     */   {
/* 206 */     return parseMimeMsgSoap(zsc, octxt, mbox, msgElem, additionalParts, NO_INV_ALLOWED_PARSER, out);
/*     */   }
/*     */   
/*     */   public static String getTextPlainContent(Element elem) {
/* 210 */     return getFirstContentByType(elem, "text/plain");
/*     */   }
/*     */   
/*     */   public static String getTextHtmlContent(Element elem) {
/* 214 */     return getFirstContentByType(elem, "text/html");
/*     */   }
/*     */   
/*     */   private static String getFirstContentByType(Element elem, String contentType)
/*     */   {
/* 219 */     if (elem == null) {
/* 220 */       return null;
/*     */     }
/*     */     
/* 223 */     if ("m".equals(elem.getName())) {
/* 224 */       elem = elem.getOptionalElement("mp");
/* 225 */       if (elem == null) {
/* 226 */         return null;
/*     */       }
/*     */     }
/*     */     
/* 230 */     String type = elem.getAttribute("ct", contentType).trim().toLowerCase();
/* 231 */     if (type.equals(contentType))
/* 232 */       return elem.getAttribute("content", null);
/* 233 */     if (type.startsWith("multipart/")) {
/* 234 */       for (Element childElem : elem.listElements("mp")) {
/* 235 */         String text = getFirstContentByType(childElem, contentType);
/* 236 */         if (text != null) {
/* 237 */           return text;
/*     */         }
/*     */       }
/*     */     }
/* 241 */     return null;
/*     */   }
/*     */   
/*     */   private static class ParseMessageContext
/*     */   {
/*     */     ParseMimeMessage.MimeMessageData out;
/*     */     ZimbraSoapContext zsc;
/*     */     OperationContext octxt;
/*     */     Mailbox mbox;
/*     */     boolean use2231;
/*     */     String defaultCharset;
/*     */     long size;
/*     */     long maxSize;
/*     */     
/*     */     ParseMessageContext()
/*     */     {
/*     */       try {
/* 258 */         Config config = Provisioning.getInstance().getConfig();
/* 259 */         this.maxSize = config.getLongAttr("zimbraMtaMaxMessageSize", -1L);
/*     */       } catch (ServiceException e) {
/* 261 */         ZimbraLog.soap.warn("Unable to determine max message size.  Disabling limit check.", e);
/*     */       }
/* 263 */       if (this.maxSize < 0L) {
/* 264 */         this.maxSize = Long.MAX_VALUE;
/*     */       }
/*     */     }
/*     */     
/*     */     void incrementSize(String name, long numBytes) throws MailServiceException {
/* 269 */       this.size += numBytes;
/* 270 */       ZimbraLog.soap.debug("Adding %s, incrementing size by %d to %d.", new Object[] { name, Long.valueOf(numBytes), Long.valueOf(this.size) });
/* 271 */       if ((this.maxSize != 0L) && (this.size > this.maxSize)) {
/* 272 */         throw MailServiceException.MESSAGE_TOO_BIG(this.maxSize, this.size);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static MimeMessage parseMimeMsgSoap(ZimbraSoapContext zsc, OperationContext octxt, Mailbox mbox, Element msgElem, MimeBodyPart[] additionalParts, InviteParser inviteParser, MimeMessageData out)
/*     */     throws ServiceException
/*     */   {
/* 290 */     assert (msgElem.getName().equals("m"));
/*     */     
/* 292 */     Account target = DocumentHandler.getRequestedAccount(zsc);
/* 293 */     ParseMessageContext ctxt = new ParseMessageContext();
/* 294 */     ctxt.out = out;
/* 295 */     ctxt.zsc = zsc;
/* 296 */     ctxt.octxt = octxt;
/* 297 */     ctxt.mbox = mbox;
/* 298 */     ctxt.use2231 = target.isPrefUseRfc2231();
/* 299 */     ctxt.defaultCharset = target.getPrefMailDefaultCharset();
/* 300 */     if (Strings.isNullOrEmpty(ctxt.defaultCharset)) {
/* 301 */       ctxt.defaultCharset = "utf-8";
/*     */     }
/*     */     try
/*     */     {
/* 305 */       MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(target));
/* 306 */       MimeMultipart mmp = null;
/*     */       
/* 308 */       Element partElem = msgElem.getOptionalElement("mp");
/* 309 */       Element attachElem = msgElem.getOptionalElement("attach");
/* 310 */       Element inviteElem = msgElem.getOptionalElement("inv");
/*     */       
/* 312 */       boolean hasContent = (partElem != null) || (inviteElem != null) || (additionalParts != null);
/* 313 */       boolean isMultipart = attachElem != null;
/* 314 */       if (isMultipart) {
/* 315 */         mmp = new ZMimeMultipart("mixed");
/* 316 */         mm.setContent(mmp);
/*     */       }
/*     */       
/*     */ 
/* 320 */       MimeBodyPart[] alternatives = null;
/*     */       
/* 322 */       if (inviteElem != null) {
/* 323 */         int additionalLen = 0;
/* 324 */         if (additionalParts != null) {
/* 325 */           additionalLen += additionalParts.length;
/*     */         }
/* 327 */         alternatives = new MimeBodyPart[additionalLen + 1];
/* 328 */         int curAltPart = 0;
/*     */         
/*     */ 
/* 331 */         InviteParserResult result = inviteParser.parse(zsc, octxt, mbox.getAccount(), inviteElem);
/*     */         
/* 333 */         if ((partElem != null) && (result.mCal != null))
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 339 */           String desc = getTextPlainContent(partElem);
/* 340 */           String html = getTextHtmlContent(partElem);
/* 341 */           result.mCal.addDescription(desc, html);
/* 342 */           if (result.mInvite != null)
/*     */           {
/*     */ 
/* 345 */             if (((desc != null) && (desc.length() > 0)) || ((html != null) && (html.length() > 0))) {
/* 346 */               result.mInvite.setDescription(desc, html);
/* 347 */               if ((desc != null) && (desc.length() > 0)) {
/* 348 */                 result.mInvite.setFragment(Fragment.getFragment(desc, true));
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/* 353 */         MimeBodyPart mbp = CalendarMailSender.makeICalIntoMimePart(result.mCal);
/* 354 */         alternatives[(curAltPart++)] = mbp;
/*     */         
/* 356 */         if (additionalParts != null) {
/* 357 */           for (int i = 0; i < additionalParts.length; i++) {
/* 358 */             alternatives[(curAltPart++)] = additionalParts[i];
/*     */           }
/*     */         }
/*     */       } else {
/* 362 */         alternatives = additionalParts;
/*     */       }
/*     */       
/*     */ 
/* 366 */       if (hasContent) {
/* 367 */         setContent(mm, mmp, partElem != null ? partElem : inviteElem, alternatives, ctxt);
/*     */       }
/*     */       
/*     */ 
/* 371 */       if ((isMultipart) && (attachElem != null)) {
/* 372 */         handleAttachments(attachElem, mmp, ctxt, null);
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 377 */       MessageAddresses maddrs = new MessageAddresses();
/* 378 */       Set<String> headerNames = ImmutableSet.copyOf(Provisioning.getInstance().getConfig().getCustomMimeHeaderNameAllowed());
/* 379 */       for (Element elem : msgElem.listElements()) {
/* 380 */         String eName = elem.getName();
/* 381 */         if (!eName.equals("attach"))
/*     */         {
/* 383 */           if (!eName.equals("mp"))
/*     */           {
/* 385 */             if (eName.equals("e")) {
/* 386 */               maddrs.add(elem, ctxt.defaultCharset);
/* 387 */             } else if (!eName.equals("irt"))
/*     */             {
/* 389 */               if (!eName.equals("su"))
/*     */               {
/* 391 */                 if (eName.equals("fr")) {
/* 392 */                   ZimbraLog.soap.debug("Ignoring message fragment data");
/* 393 */                 } else if (!eName.equals("inv"))
/*     */                 {
/* 395 */                   if (!eName.equals("tz"))
/*     */                   {
/* 397 */                     if (eName.equals("header")) {
/* 398 */                       String name = elem.getAttribute("name");
/* 399 */                       if (headerNames.contains(name)) {
/* 400 */                         mm.addHeader(name, MimeHeader.escape(elem.getText(), Charsets.UTF_8, true));
/*     */                       } else {
/* 402 */                         throw ServiceException.INVALID_REQUEST("header '" + name + "' not allowed", null);
/*     */                       }
/*     */                     } else {
/* 405 */                       ZimbraLog.soap.warn("unsupported child element '%s' under parent %s", new Object[] { elem.getName(), msgElem.getName() });
/*     */                     } } } }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/* 411 */       String subject = msgElem.getAttribute("su", "");
/* 412 */       mm.setSubject(subject, CharsetUtil.checkCharset(subject, ctxt.defaultCharset));
/*     */       
/* 414 */       String irt = cleanReference(msgElem.getAttribute("irt", null));
/* 415 */       if (irt != null) {
/* 416 */         mm.setHeader("In-Reply-To", irt);
/*     */       }
/*     */       
/*     */ 
/* 420 */       if (!maddrs.isEmpty()) {
/* 421 */         addAddressHeaders(mm, maddrs);
/*     */       }
/*     */       
/* 424 */       if ((!hasContent) && (!isMultipart)) {
/* 425 */         mm.setText("", "us-ascii");
/*     */       }
/*     */       
/* 428 */       String flagStr = msgElem.getAttribute("f", "");
/* 429 */       if (flagStr.indexOf(Flag.toChar(Flag.ID_HIGH_PRIORITY)) != -1) {
/* 430 */         mm.addHeader("X-Priority", "1");
/* 431 */         mm.addHeader("Importance", "high");
/* 432 */       } else if (flagStr.indexOf(Flag.toChar(Flag.ID_LOW_PRIORITY)) != -1) {
/* 433 */         mm.addHeader("X-Priority", "5");
/* 434 */         mm.addHeader("Importance", "low");
/*     */       }
/*     */       
/*     */ 
/* 438 */       mm.saveChanges();
/* 439 */       return mm;
/*     */     } catch (UnsupportedEncodingException e) {
/* 441 */       throw ServiceException.FAILURE("UnsupportedEncodingExecption", e);
/*     */     } catch (SendFailedException e) {
/* 443 */       MailSender.SafeSendFailedException ssfe = new MailSender.SafeSendFailedException(e);
/* 444 */       throw ServiceException.FAILURE("SendFailure", ssfe);
/*     */     } catch (MessagingException e) {
/* 446 */       throw ServiceException.FAILURE("MessagingExecption", e);
/*     */     } catch (IOException e) {
/* 448 */       throw ServiceException.FAILURE("IOExecption", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void handleAttachments(Element attachElem, MimeMultipart mmp, ParseMessageContext ctxt, String contentID) throws ServiceException, MessagingException, IOException
/*     */   {
/* 454 */     if (contentID != null) {
/* 455 */       contentID = '<' + contentID + '>';
/*     */     }
/*     */     
/* 458 */     String attachIds = attachElem.getAttribute("aid", null);
/* 459 */     if (attachIds != null) {
/* 460 */       for (String uploadId : attachIds.split(",")) {
/* 461 */         FileUploadServlet.Upload up = FileUploadServlet.fetchUpload(ctxt.zsc.getAuthtokenAccountId(), uploadId, ctxt.zsc.getAuthToken());
/* 462 */         if (up == null) {
/* 463 */           throw MailServiceException.NO_SUCH_UPLOAD(uploadId);
/*     */         }
/* 465 */         attachUpload(mmp, up, contentID, ctxt, null, null);
/* 466 */         ctxt.out.addUpload(up);
/*     */       }
/*     */     }
/*     */     
/* 470 */     for (Element elem : attachElem.listElements()) {
/* 471 */       String attachType = elem.getName();
/* 472 */       boolean optional = elem.getAttributeBool("optional", false);
/*     */       try {
/* 474 */         if (attachType.equals("mp")) {
/* 475 */           ItemId iid = new ItemId(elem.getAttribute("mid"), ctxt.zsc);
/* 476 */           String part = elem.getAttribute("part");
/* 477 */           attachPart(mmp, iid, part, contentID, ctxt);
/* 478 */         } else if (attachType.equals("m")) {
/* 479 */           ItemId iid = new ItemId(elem.getAttribute("id"), ctxt.zsc);
/* 480 */           attachMessage(mmp, iid, contentID, ctxt);
/* 481 */         } else if (attachType.equals("cn")) {
/* 482 */           ItemId iid = new ItemId(elem.getAttribute("id"), ctxt.zsc);
/* 483 */           attachContact(mmp, iid, contentID, ctxt);
/* 484 */         } else if (attachType.equals("doc")) {
/* 485 */           String path = elem.getAttribute("path", null);
/* 486 */           if (path != null) {
/* 487 */             attachDocument(mmp, path, contentID, ctxt);
/*     */           } else {
/* 489 */             ItemId iid = new ItemId(elem.getAttribute("id"), ctxt.zsc);
/* 490 */             int version = (int)elem.getAttributeLong("ver", 0L);
/* 491 */             attachDocument(mmp, iid, version, contentID, ctxt);
/*     */           }
/*     */         }
/*     */       } catch (MailServiceException.NoSuchItemException nsie) {
/* 495 */         if (!optional)
/* 496 */           throw nsie;
/* 497 */         ZimbraLog.soap.info("skipping missing optional attachment: " + elem);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static void setContent(MimeMessage mm, MimeMultipart mmp, Element elem, MimeBodyPart[] alternatives, ParseMessageContext ctxt)
/*     */     throws MessagingException, ServiceException, IOException
/*     */   {
/* 511 */     String type = elem.getAttribute("ct", "text/plain").trim();
/* 512 */     ContentType ctype = new ContentType(type, ctxt.use2231).cleanup();
/*     */     
/*     */ 
/* 515 */     if (ctype.getPrimaryType().equals("multipart"))
/*     */     {
/* 517 */       setMultipartContent(ctype.getSubType(), mm, mmp, elem, alternatives, ctxt);
/* 518 */       return;
/*     */     }
/*     */     
/* 521 */     Element inline = elem.getOptionalElement("attach");
/* 522 */     if (inline != null) {
/* 523 */       handleAttachments(inline, mmp, ctxt, elem.getAttribute("ci", null));
/* 524 */       return;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 530 */     if (alternatives != null)
/*     */     {
/* 532 */       MimeMultipart mmpNew = new ZMimeMultipart("alternative");
/* 533 */       if (mmp == null) {
/* 534 */         mm.setContent(mmpNew);
/*     */       } else {
/* 536 */         MimeBodyPart mbpWrapper = new ZMimeBodyPart();
/* 537 */         mbpWrapper.setContent(mmpNew);
/* 538 */         mmp.addBodyPart(mbpWrapper);
/*     */       }
/* 540 */       mmp = mmpNew;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 546 */     String text = elem.getAttribute("content", "");
/* 547 */     byte[] raw = text.getBytes(Charsets.UTF_8);
/* 548 */     if ((raw.length > 0) || (!LC.mime_exclude_empty_content.booleanValue()) || (ctype.getPrimaryType().equals("text"))) {
/* 549 */       ctxt.incrementSize("message body", raw.length);
/*     */       
/*     */ 
/* 552 */       String charset = CharsetUtil.checkCharset(text, ctxt.defaultCharset);
/* 553 */       ctype.setCharset(charset).setParameter("charset", charset);
/*     */       
/* 555 */       Object content = ctype.getContentType().equals("message/rfc822") ? new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw)) : text;
/*     */       
/* 557 */       if (mmp != null) {
/* 558 */         MimeBodyPart mbp = new ZMimeBodyPart();
/* 559 */         mbp.setContent(content, ctype.toString());
/* 560 */         mmp.addBodyPart(mbp);
/*     */       } else {
/* 562 */         mm.setContent(content, ctype.toString());
/*     */       }
/*     */     }
/* 565 */     if (alternatives != null) {
/* 566 */       for (int i = 0; i < alternatives.length; i++) {
/* 567 */         ctxt.incrementSize("alternative body", alternatives[i].getSize());
/* 568 */         mmp.addBodyPart(alternatives[i]);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void setMultipartContent(String subType, MimeMessage mm, MimeMultipart mmp, Element elem, MimeBodyPart[] alternatives, ParseMessageContext ctxt)
/*     */     throws MessagingException, ServiceException, IOException
/*     */   {
/* 576 */     if ((alternatives == null) || (subType.equals("alternative")))
/*     */     {
/*     */ 
/*     */ 
/* 580 */       MimeMultipart mmpNew = new ZMimeMultipart(subType);
/* 581 */       if (mmp == null)
/*     */       {
/* 583 */         mm.setContent(mmpNew);
/*     */       }
/*     */       else {
/* 586 */         MimeBodyPart mbpWrapper = new ZMimeBodyPart();
/* 587 */         mbpWrapper.setContent(mmpNew);
/* 588 */         mmp.addBodyPart(mbpWrapper);
/*     */       }
/*     */       
/*     */ 
/* 592 */       for (Element subpart : elem.listElements()) {
/* 593 */         setContent(mm, mmpNew, subpart, null, ctxt);
/*     */       }
/*     */       
/*     */ 
/* 597 */       if (alternatives != null) {
/* 598 */         for (int i = 0; i < alternatives.length; i++) {
/* 599 */           ctxt.incrementSize("alternative", alternatives[i].getSize());
/* 600 */           mmpNew.addBodyPart(alternatives[i]);
/*     */         }
/*     */       }
/*     */     }
/*     */     else {
/* 605 */       MimeMultipart mmpNew = new ZMimeMultipart("alternative");
/* 606 */       if (mmp == null) {
/* 607 */         mm.setContent(mmpNew);
/*     */       } else {
/* 609 */         MimeBodyPart mbpWrapper = new ZMimeBodyPart();
/* 610 */         mbpWrapper.setContent(mmpNew);
/* 611 */         mmp.addBodyPart(mbpWrapper);
/*     */       }
/*     */       
/*     */ 
/* 615 */       setContent(mm, mmpNew, elem, null, ctxt);
/*     */       
/*     */ 
/* 618 */       for (int i = 0; i < alternatives.length; i++) {
/* 619 */         ctxt.incrementSize("alternative", alternatives[i].getSize());
/* 620 */         mmpNew.addBodyPart(alternatives[i]);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void attachUpload(MimeMultipart mmp, FileUploadServlet.Upload up, String contentID, ParseMessageContext ctxt, ContentType ctypeOverride, String contentDescription)
/*     */     throws ServiceException, MessagingException
/*     */   {
/* 628 */     ctxt.incrementSize("upload " + up.getName(), (up.getSize() * 1.33D));
/*     */     
/*     */ 
/* 631 */     StringBuffer info = new StringBuffer();
/* 632 */     UploadScanner.Result result = UploadScanner.accept(up, info);
/* 633 */     if (result == UploadScanner.REJECT)
/* 634 */       throw MailServiceException.UPLOAD_REJECTED(up.getName(), info.toString());
/* 635 */     if (result == UploadScanner.ERROR) {
/* 636 */       throw MailServiceException.SCAN_ERROR(up.getName());
/*     */     }
/* 638 */     String filename = up.getName();
/*     */     
/*     */ 
/* 641 */     MimeBodyPart mbp = new ForceBase64MimeBodyPart();
/*     */     
/* 643 */     UploadDataSource uds = new UploadDataSource(up);
/* 644 */     if ((ctypeOverride != null) && (!ctypeOverride.equals(""))) {
/* 645 */       uds.setContentType(ctypeOverride);
/*     */     }
/* 647 */     mbp.setDataHandler(new DataHandler(uds));
/*     */     
/*     */ 
/* 650 */     ContentType ctype = ctypeOverride;
/* 651 */     ContentDisposition cdisp = new ContentDisposition("attachment", ctxt.use2231);
/* 652 */     if (ctype == null) {
/* 653 */       ctype = new ContentType(up.getContentType() == null ? "application/octet-stream" : up.getContentType());
/* 654 */       ctype.cleanup().setParameter("name", filename);
/* 655 */       cdisp.setParameter("filename", filename);
/*     */     }
/* 657 */     mbp.setHeader("Content-Type", ctype.setCharset(ctxt.defaultCharset).toString());
/* 658 */     mbp.setHeader("Content-Disposition", cdisp.setCharset(ctxt.defaultCharset).toString());
/* 659 */     if (contentDescription != null) {
/* 660 */       mbp.setHeader("Content-Description", contentDescription);
/*     */     }
/* 662 */     if (ctype.getContentType().equals("application/pdf")) {
/* 663 */       mbp.setHeader("Content-Transfer-Encoding", "base64");
/*     */     }
/* 665 */     mbp.setContentID(contentID);
/*     */     
/*     */ 
/* 668 */     mmp.addBodyPart(mbp);
/*     */   }
/*     */   
/*     */   private static void attachRemoteItem(MimeMultipart mmp, ItemId iid, String contentID, ParseMessageContext ctxt, Map<String, String> params, ContentType ctypeOverride) throws ServiceException, MessagingException
/*     */   {
/*     */     try
/*     */     {
/* 675 */       FileUploadServlet.Upload up = UserServlet.getRemoteResourceAsUpload(ctxt.zsc.getAuthToken(), iid, params);
/* 676 */       ctxt.out.addFetch(up);
/* 677 */       attachUpload(mmp, up, contentID, ctxt, ctypeOverride, null);
/* 678 */       return;
/*     */     } catch (IOException ioe) {
/* 680 */       throw ServiceException.FAILURE("can't serialize remote item", ioe);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void attachMessage(MimeMultipart mmp, ItemId iid, String contentID, ParseMessageContext ctxt)
/*     */     throws MessagingException, ServiceException
/*     */   {
/* 687 */     if (!iid.isLocal()) {
/* 688 */       attachRemoteItem(mmp, iid, contentID, ctxt, Collections.EMPTY_MAP, new ContentType("message/rfc822"));
/* 689 */       return;
/*     */     }
/*     */     
/* 692 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
/* 693 */     Message msg = mbox.getMessageById(ctxt.octxt, iid.getId());
/* 694 */     ctxt.incrementSize("attached message", msg.getSize());
/*     */     
/* 696 */     MimeBodyPart mbp = new ZMimeBodyPart();
/* 697 */     mbp.setDataHandler(new DataHandler(new MailboxBlobDataSource(msg.getBlob())));
/* 698 */     mbp.setHeader("Content-Type", "message/rfc822");
/* 699 */     mbp.setHeader("Content-Disposition", "attachment");
/* 700 */     mbp.setContentID(contentID);
/* 701 */     mmp.addBodyPart(mbp);
/*     */   }
/*     */   
/*     */   static
/*     */   {
/* 173 */     NO_INV_ALLOWED_PARSER = new InviteParser()
/*     */     {
/*     */       public ParseMimeMessage.InviteParserResult parseInviteElement(ZimbraSoapContext zsc, OperationContext octxt, Account account, Element inviteElem) throws ServiceException
/*     */       {
/* 177 */         throw ServiceException.INVALID_REQUEST("No <inv> element allowed for this request", null);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 703 */     };
/* 704 */     FETCH_CONTACT_PARAMS = new HashMap(3);
/*     */     
/* 706 */     FETCH_CONTACT_PARAMS.put("fmt", "vcf");
/*     */   }
/*     */   
/*     */   private static void attachContact(MimeMultipart mmp, ItemId iid, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException
/*     */   {
/* 711 */     if (!iid.isLocal()) {
/* 712 */       attachRemoteItem(mmp, iid, contentID, ctxt, FETCH_CONTACT_PARAMS, null);
/* 713 */       return;
/*     */     }
/*     */     
/* 716 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
/* 717 */     VCard vcf = VCard.formatContact(mbox.getContactById(ctxt.octxt, iid.getId()));
/*     */     
/* 719 */     ctxt.incrementSize("contact", vcf.getFormatted().length());
/* 720 */     String filename = vcf.fn + ".vcf";
/* 721 */     String charset = CharsetUtil.checkCharset(vcf.getFormatted(), ctxt.defaultCharset);
/*     */     
/* 723 */     MimeBodyPart mbp = new ZMimeBodyPart();
/* 724 */     mbp.setText(vcf.getFormatted(), charset);
/* 725 */     mbp.setHeader("Content-Type", new ContentType("text/x-vcard", ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("name", filename).setParameter("charset", charset).toString());
/* 726 */     mbp.setHeader("Content-Disposition", new ContentDisposition("attachment", ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("filename", filename).toString());
/* 727 */     mbp.setContentID(contentID);
/* 728 */     mmp.addBodyPart(mbp);
/*     */   }
/*     */   
/*     */   private static void attachDocument(MimeMultipart mmp, ItemId iid, int version, String contentID, ParseMessageContext ctxt)
/*     */     throws MessagingException, ServiceException
/*     */   {
/* 734 */     if (!iid.isLocal()) {
/* 735 */       Map<String, String> params = Collections.EMPTY_MAP;
/* 736 */       if (version > 0) {
/* 737 */         params = new HashMap();
/* 738 */         params.put("ver", Integer.toString(version));
/*     */       }
/* 740 */       attachRemoteItem(mmp, iid, contentID, ctxt, params, null);
/* 741 */       return;
/*     */     }
/*     */     
/* 744 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
/*     */     Document doc;
/* 746 */     Document doc; if (version > 0) {
/* 747 */       doc = (Document)mbox.getItemRevision(ctxt.octxt, iid.getId(), MailItem.Type.DOCUMENT, version);
/*     */     } else {
/* 749 */       doc = mbox.getDocumentById(ctxt.octxt, iid.getId());
/*     */     }
/* 751 */     attachDocument(mmp, doc, contentID, ctxt);
/*     */   }
/*     */   
/*     */   private static void attachDocument(MimeMultipart mmp, String path, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException
/*     */   {
/* 756 */     MailItem item = null;
/*     */     try
/*     */     {
/* 759 */       item = ctxt.mbox.getItemByPath(ctxt.octxt, path);
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException nsie) {}
/* 762 */     if (item == null)
/*     */     {
/* 764 */       Pair<Folder, String> match = ctxt.mbox.getFolderByPathLongestMatch(ctxt.octxt, 1, path);
/* 765 */       if (!(match.getFirst() instanceof Mountpoint)) {
/* 766 */         throw MailServiceException.NO_SUCH_DOC(path);
/*     */       }
/*     */       
/* 769 */       Map<String, String> params = new HashMap(3);
/* 770 */       params.put("name", match.getSecond());
/* 771 */       attachRemoteItem(mmp, ((Mountpoint)match.getFirst()).getTarget(), contentID, ctxt, params, null);
/* 772 */       return;
/*     */     }
/*     */     
/*     */ 
/* 776 */     if (!(item instanceof Document))
/* 777 */       throw MailServiceException.NO_SUCH_DOC(path);
/* 778 */     attachDocument(mmp, (Document)item, contentID, ctxt);
/*     */   }
/*     */   
/*     */   private static void attachDocument(MimeMultipart mmp, Document doc, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException
/*     */   {
/* 783 */     ctxt.incrementSize("attached document", (doc.getSize() * 1.33D));
/* 784 */     ContentType ct = new ContentType(doc.getContentType());
/* 785 */     if (MimeConstants.isZimbraDocument(ct.getContentType())) {
/* 786 */       ct = new ContentType("text/html");
/*     */     }
/*     */     
/* 789 */     MimeBodyPart mbp = new ZMimeBodyPart();
/* 790 */     mbp.setDataHandler(new DataHandler(new MailboxBlobDataSource(doc.getBlob(), ct.getContentType())));
/* 791 */     mbp.setHeader("Content-Type", ct.cleanup().setParameter("name", doc.getName()).setCharset(ctxt.defaultCharset).toString());
/* 792 */     mbp.setHeader("Content-Disposition", new ContentDisposition("attachment").setParameter("filename", doc.getName()).toString());
/* 793 */     mbp.setContentID(contentID);
/* 794 */     mmp.addBodyPart(mbp);
/*     */   }
/*     */   
/*     */   private static void attachPart(MimeMultipart mmp, ItemId iid, String part, String contentID, ParseMessageContext ctxt)
/*     */     throws IOException, MessagingException, ServiceException
/*     */   {
/* 800 */     if (!iid.isLocal()) {
/* 801 */       Map<String, String> params = new HashMap(3);
/* 802 */       params.put("part", part);
/* 803 */       attachRemoteItem(mmp, iid, contentID, ctxt, params, null);
/* 804 */       return;
/*     */     }
/*     */     
/* 807 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
/*     */     MimeMessage mm;
/* 809 */     MimeMessage mm; if (iid.hasSubpart()) {
/* 810 */       mm = mbox.getCalendarItemById(ctxt.octxt, iid.getId()).getSubpartMessage(iid.getSubpartId());
/*     */     } else {
/* 812 */       mm = mbox.getMessageById(ctxt.octxt, iid.getId()).getMimeMessage();
/*     */     }
/* 814 */     MimePart mp = Mime.getMimePart(mm, part);
/* 815 */     if (mp == null) {
/* 816 */       throw MailServiceException.NO_SUCH_PART(part);
/*     */     }
/*     */     
/* 819 */     String filename = Mime.getFilename(mp);
/* 820 */     String ctypeHdr = mp.getContentType();String contentType = null;
/* 821 */     if (ctypeHdr != null) {
/* 822 */       contentType = new ContentType(ctypeHdr, ctxt.use2231).cleanup().setCharset(ctxt.defaultCharset).setParameter("name", filename).toString();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 828 */     FileUploadServlet.Upload up = FileUploadServlet.saveUpload(mp.getInputStream(), filename, contentType, DocumentHandler.getRequestedAccount(ctxt.zsc).getId());
/* 829 */     ctxt.out.addFetch(up);
/* 830 */     String[] contentDesc = mp.getHeader("Content-Description");
/* 831 */     attachUpload(mmp, up, contentID, ctxt, null, (contentDesc == null) || (contentDesc.length == 0) ? null : contentDesc[0]);
/*     */   }
/*     */   
/*     */   static final class MessageAddresses
/*     */   {
/* 836 */     private final HashMap<String, Object> addrs = new HashMap();
/*     */     
/*     */     public void add(Element elem, String defaultCharset) throws ServiceException, UnsupportedEncodingException {
/* 839 */       String emailAddress = IDNUtil.toAscii(elem.getAttribute("a"));
/* 840 */       String personalName = elem.getAttribute("p", null);
/* 841 */       String addressType = elem.getAttribute("t");
/*     */       
/* 843 */       InternetAddress addr = new JavaMailInternetAddress(emailAddress, personalName, CharsetUtil.checkCharset(personalName, defaultCharset));
/*     */       
/*     */ 
/* 846 */       Object content = this.addrs.get(addressType);
/* 847 */       if ((content == null) || (addressType.equals(ToXML.EmailType.FROM.toString())) || (addressType.equals(ToXML.EmailType.SENDER.toString())))
/*     */       {
/* 849 */         this.addrs.put(addressType, addr);
/* 850 */       } else if ((content instanceof List))
/*     */       {
/* 852 */         List<InternetAddress> list = (List)content;
/* 853 */         list.add(addr);
/*     */       } else {
/* 855 */         List<InternetAddress> list = new ArrayList();
/* 856 */         list.add((InternetAddress)content);
/* 857 */         list.add(addr);
/* 858 */         this.addrs.put(addressType, list);
/*     */       }
/*     */     }
/*     */     
/*     */     public InternetAddress[] get(String addressType)
/*     */     {
/* 864 */       Object content = this.addrs.get(addressType);
/* 865 */       if (content == null)
/* 866 */         return null;
/* 867 */       if ((content instanceof InternetAddress)) {
/* 868 */         return new InternetAddress[] { (InternetAddress)content };
/*     */       }
/* 870 */       return (InternetAddress[])((List)content).toArray(new InternetAddress[0]);
/*     */     }
/*     */     
/*     */     public boolean isEmpty()
/*     */     {
/* 875 */       return this.addrs.isEmpty();
/*     */     }
/*     */   }
/*     */   
/*     */   private static void addAddressHeaders(MimeMessage mm, MessageAddresses maddrs) throws MessagingException {
/* 880 */     InternetAddress[] addrs = maddrs.get(ToXML.EmailType.TO.toString());
/* 881 */     if ((addrs != null) && (addrs.length > 0)) {
/* 882 */       mm.addRecipients(Message.RecipientType.TO, addrs);
/*     */     }
/*     */     
/* 885 */     addrs = maddrs.get(ToXML.EmailType.CC.toString());
/* 886 */     if ((addrs != null) && (addrs.length > 0)) {
/* 887 */       mm.addRecipients(Message.RecipientType.CC, addrs);
/*     */     }
/*     */     
/* 890 */     addrs = maddrs.get(ToXML.EmailType.BCC.toString());
/* 891 */     if ((addrs != null) && (addrs.length > 0)) {
/* 892 */       mm.addRecipients(Message.RecipientType.BCC, addrs);
/*     */     }
/*     */     
/* 895 */     addrs = maddrs.get(ToXML.EmailType.FROM.toString());
/* 896 */     if ((addrs != null) && (addrs.length == 1)) {
/* 897 */       mm.setFrom(addrs[0]);
/*     */     }
/*     */     
/* 900 */     addrs = maddrs.get(ToXML.EmailType.SENDER.toString());
/* 901 */     if ((addrs != null) && (addrs.length == 1)) {
/* 902 */       mm.setSender(addrs[0]);
/*     */     }
/*     */     
/* 905 */     addrs = maddrs.get(ToXML.EmailType.REPLY_TO.toString());
/* 906 */     if ((addrs != null) && (addrs.length > 0)) {
/* 907 */       mm.setReplyTo(addrs);
/*     */     }
/*     */     
/* 910 */     addrs = maddrs.get(ToXML.EmailType.READ_RECEIPT.toString());
/* 911 */     if ((addrs != null) && (addrs.length > 0)) {
/* 912 */       mm.addHeader("Disposition-Notification-To", InternetAddress.toString(addrs));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static String cleanReference(String refStr)
/*     */   {
/* 920 */     if (refStr == null) {
/* 921 */       return null;
/*     */     }
/* 923 */     String reference = refStr.trim();
/* 924 */     if (reference.isEmpty()) {
/* 925 */       return null;
/*     */     }
/*     */     
/* 928 */     if (!reference.startsWith("<")) {
/* 929 */       reference = "<" + reference;
/*     */     }
/* 931 */     if (!reference.endsWith(">")) {
/* 932 */       reference = reference + ">";
/*     */     }
/* 934 */     return reference;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ParseMimeMessage.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */